﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.ComMethod;

namespace FortuneHeaven.Web.Pages.UserCenter
{
    public partial class ChangePassword : CheckAuthPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.hidden);
                ltName.Text = AuthorityCenter.CurrentUserName;
            }
        }

        public void SavePassword(object sender, EventArgs e)
        {
            try
            {
                
                AccountsInfoBO acountBO = new AccountsInfoBO();
                if (!string.IsNullOrEmpty(AuthorityCenter.CurrentUserName))
                {
                    AccountsInfo acount = acountBO.CheckUserPwd(AuthorityCenter.CurrentUserName, CommonEncrypt.ClientEncrypt(this.txtCurrentPassword.Text));
             
                    if (acount == null)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "ErrorPassword", "alert('密码不正确')", true);
                        this.txtCurrentPassword.Text = "";
                        return;
                    }
                    if (this.txtNewPassword.Text != this.txtReinput.Text)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "ErrorReinput", "alert('两次输入密码不一致')", true);
                        this.txtReinput.Text = "";
                        return;
                    }
                    if (this.txtNewPassword.Text.Length < 6 || this.txtNewPassword.Text.Length > 15)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "Errorinput", "alert('新密码必须由6-15位数字或字母组成')", true);
                        this.txtReinput.Text = "";
                        return;
                    }
                    acountBO.SetNewPassword(AuthorityCenter.CurrentUserID, CommonEncrypt.ClientEncrypt(this.txtNewPassword.Text));
                    ClientScript.RegisterStartupScript(this.GetType(), "Success", "alert('修改成功')", true);
                }
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "ErrorServer", "alert('服务器发生错误。请稍后再试。')", true);
            }
        }
    }
}